package  com.syqc.sensor.weight

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.utils.MLog
import com.syqc.entity.OilCfg
import com.syqc.entity.OilDay
import com.syqc.entity.StatsType
import com.syqc.net.ServiceHelper
import com.syqc.base.BaseReportViewModel
import com.syqc.comlib.utils.GsonUtil
import com.syqc.monitor.R
import com.syqc.utils.DataUtil
import com.syqc.utils.ToolsUtil

/**
 *@Author pc_hfjs
 *@Date 2022/1/19 17:31
 *@Desc
 */
class WeightStatsViewModel : BaseReportViewModel(true, true) {
    private val _oilStatsLive = MutableLiveData<Boolean>()
    val oilStatsLive: LiveData<Boolean> = _oilStatsLive
    override fun getData(carId: String, startTime: String, endTime: String) {
        super.getData(carId, startTime, endTime)
        toastLoading(true)
        getOilStats(carId, startTime, endTime)
    }

    //获取油量
    private fun getOilStats(carId: String, startTime: String, endTime: String) {
        ServiceHelper.getRepository()
            .getDayStats(2, carId, startTime, endTime)
            .autoDispose(this)
            .subscribe({
                MLog.e("油量数据===== $it")
                if (isHasListData(it, R.string.toast_no_analog_load)) {
                    toastLoading(false)
                    return@subscribe
                }
                if (it[0].cfg == null) {
                    toast(R.string.toast_no_set_analog)
                    toastLoading(false)
                    return@subscribe
                }
                handleData(it)

            }, error)
    }

    private val types = ArrayList<OilCfg>()

    private fun handleData(data: ArrayList<OilDay>) {
        data.forEach { dayInfo ->
            val oilCfg = GsonUtil.formObject<OilCfg>(dayInfo.cfg)
            if (oilCfg.name != null) {
                hasType(oilCfg)
                dayInfo.name = oilCfg.name
                dayInfo.precision = oilCfg.precision
                dayInfo.unit = oilCfg.unit
                dayInfo.upTotal = ToolsUtil.formitNumberD(oilCfg.precision, dayInfo.upTotal)
                dayInfo.downTotal =
                    ToolsUtil.formitNumberD(dayInfo.precision, dayInfo.downTotal)
                dayInfo.mileKm =
                    ToolsUtil.formitNumberD(dayInfo.precision, dayInfo.mile / 1000.0)
            }
        }
        val arrayList = ArrayList<StatsType>()
        types.sortBy { it.typeId }
        types.forEach { cfg ->
            val statsType = StatsType(cfg.typeId!!, cfg.name!!, cfg.unit, cfg.precision)
            data.forEach { day ->
                if (day.typeId == cfg.typeId)
                    statsType.info.add(day)
            }
            arrayList.add(statsType)
        }

        DataUtil.dayStats = arrayList
        toastLoading(false)
        _oilStatsLive.postValue(true)
    }

    /**
     *判断是否含有同类型
     *
     * @param oilCfg
     * @return
     */
    private fun hasType(oilCfg: OilCfg): Boolean {
        for (type in types) {
            if (type.typeId == oilCfg.typeId) return true
        }
        types.add(oilCfg)
        return false
    }
}